interface AnimalInterface {
  name: string;
}
class Animal implements AnimalInterface { //类实现接口
  name: string;
  constructor(name: string) {
    this.name = name;
  }
}

let Animal1 = new Animal('动物');
console.log(Animal1.name);

class Dog extends Animal {
  constructor(name) {
    super(name);
  }
  code: number = 1;
}
let dog1 = new Dog('狗狗');
console.log(dog1);

class Tiger extends Animal {
  constructor(name) {
    super(name);
  }
  code: number = 0;
}
let tiger1 = new Tiger('老虎');
console.log(tiger1);

// 用父类断言为子类?
// function canEat(animal: Animal) {
//   if (animal .code === 'number') {//1类型“Animal”上不存在属性“code”。code属性是到了子类上才定义的
//     return true;
//   }
//   return '不能吃';
// }
// 用父类断言为子类?
function canEat(animal: Animal) {
  if ((animal as Dog | Tiger).code === undefined) {
    console.log('传入的不是Dog和Tiger的实例');

    return false;
  } else if ((animal as Dog | Tiger).code === 1) {
    console.log('能吃');

    return true;
  } else {
    console.log('不能吃');
    return false;
  }
}
canEat(new Animal('动物1'));
canEat(new Dog('狗狗'));
canEat(new Tiger('老虎'));
